//
//  UPLoanHistoryCell.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/23.
//

#import "UPLoanHistoryCell.h"

@interface UPLoanHistoryCell()

@property (nonatomic, strong) UIImageView *containerImageView;
@property (nonatomic, strong) UILabel *tagLabel;
@property (nonatomic, strong) UIImageView *tipsImageView;
@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) UILabel *loanAmountLabel;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIButton *applyButton;

@end

@implementation UPLoanHistoryCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.containerImageView];
    [self addSubview:self.tagLabel];
    [self addSubview:self.tipsImageView];
    [self addSubview:self.tipsLabel];
    [self addSubview:self.loanAmountLabel];
    [self addSubview:self.numberLabel];
    [self addSubview:self.dateLabel];
    [self addSubview:self.applyButton];
}

- (void)setupConstraints {
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self);
        make.top.equalTo(self).offset(22);
    }];
    
    [self.tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.equalTo(self);
        make.height.mas_equalTo(43);
        make.width.mas_equalTo(232);
    }];
    
    [self.tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tipsLabel);
        make.height.width.mas_equalTo(14);
        make.trailing.equalTo(self.tipsLabel.mas_leading).offset(-5);
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tagLabel.mas_bottom).offset(13);
        make.centerX.equalTo(self.tagLabel).offset(7);
        make.height.mas_equalTo(25);
    }];
    
    [self.loanAmountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tipsLabel.mas_bottom).offset(20);
        make.leading.equalTo(self).offset(35);
        make.height.mas_equalTo(25);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loanAmountLabel.mas_bottom).offset(10);
        make.leading.equalTo(self.loanAmountLabel);
        make.height.mas_equalTo(25);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.numberLabel.mas_bottom).offset(10);
        make.leading.equalTo(self.loanAmountLabel);
        make.height.mas_equalTo(25);
    }];
    
    [self.applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.dateLabel);
        make.trailing.equalTo(self).offset(-10);
        make.height.mas_equalTo(36);
    }];
}

- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"history_cell_bg"];
        [self.contentView addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UILabel *)tagLabel {
    if (!_tagLabel) {
        _tagLabel = [[UILabel alloc] init];
        _tagLabel.layer.masksToBounds = true;
        _tagLabel.layer.cornerRadius = 21.5;
        _tagLabel.layer.borderWidth = 2.0;
        _tagLabel.layer.borderColor = [UIColor colorWithHexString:@"#000000"].CGColor;
        _tagLabel.font = [UIFont boldSystemFontOfSize:15];
        _tagLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tagLabel;
}

- (UIImageView *)tipsImageView {
    if (!_tipsImageView) {
        _tipsImageView = [[UIImageView alloc] init];
        _tipsImageView.contentMode = UIViewContentModeScaleAspectFit;
        _tipsImageView.image = [UIImage imageNamed:@"history_tips"];
        [self.contentView addSubview:_tipsImageView];
    }
    return _tipsImageView;
}

- (UILabel *)tipsLabel {
    if (!_tipsLabel) {
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.font = [UIFont systemFontOfSize:13];
        _tipsLabel.textAlignment = NSTextAlignmentLeft;
        _tipsLabel.textColor = [[UIColor colorWithHexString:@"#FF0000"] colorWithAlphaComponent:0.5];
//        _tipsLabel.text = @"You're three days late. Pay now";
    }
    return _tipsLabel;
}

- (UILabel *)loanAmountLabel {
    if (!_loanAmountLabel) {
        _loanAmountLabel = [[UILabel alloc] init];
        _loanAmountLabel.font = [UIFont systemFontOfSize:13];
        _loanAmountLabel.textAlignment = NSTextAlignmentLeft;
        _loanAmountLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _loanAmountLabel.text = LocalizableForKey(@"Loan Amount");
    }
    return _loanAmountLabel;
}

- (UILabel *)numberLabel {
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.font = [UIFont boldSystemFontOfSize:25];
        _numberLabel.textAlignment = NSTextAlignmentLeft;
        _numberLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _numberLabel.text = LocalizableForKey(@"9000.000.00");
    }
    return _numberLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.font = [UIFont systemFontOfSize:13];
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _dateLabel.text = LocalizableForKey(@"Loan Date：10-31-2024");
    }
    return _dateLabel;
}

- (UIButton *)applyButton {
    if (!_applyButton) {
        _applyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_applyButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        [_applyButton setTitle:@"GO APPLY" forState:UIControlStateNormal];
        _applyButton.titleLabel.font = [UIFont systemFontOfSize:11 weight:400];
        [_applyButton setImage:[UIImage imageNamed:@"history_apply_arrow"] forState:UIControlStateNormal];
        _applyButton.layer.masksToBounds = true;
        _applyButton.layer.cornerRadius = 18;
        _applyButton.backgroundColor = [UIColor colorWithHexString:@"#000000"];
        _applyButton.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
        _applyButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        CGFloat imageInsetRight = 10.0f;
        _applyButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -imageInsetRight);
    }
    return _applyButton;
}

- (void)setCircumibilityModel:(HistoryCircumibilityModel *)circumibilityModel {
    _circumibilityModel = circumibilityModel;
    self.tagLabel.text = circumibilityModel.radi.carditor;
    self.dateLabel.text = [NSString stringWithFormat:@"%@: %@", circumibilityModel.radi.dextrplacead, circumibilityModel.radi.eastics];
    self.loanAmountLabel.text = circumibilityModel.calden;
    self.numberLabel.text = circumibilityModel.identifyern;
    
    NSString *rate = circumibilityModel.radi.anteaire;
    if (rate != nil && rate.length > 0) {
        self.tipsImageView.hidden = NO;
        self.tipsLabel.hidden = NO;
        self.tipsLabel.text = rate;
    } else {
        self.tipsImageView.hidden = YES;
        self.tipsLabel.hidden = YES;
    }
    
    NSString *buttonTitle = circumibilityModel.radi.treatlike;
    if (buttonTitle != nil && buttonTitle.length > 0) {
        self.applyButton.hidden = NO;
    } else {
        self.applyButton.hidden = YES;
    }
    [self.applyButton setTitle:buttonTitle forState:UIControlStateNormal];
    
    NSString *status = circumibilityModel.radi.commonative;
    NSString *tagBgColor = @"#C1E3FB";
    NSString *tagtitleColor = @"#40B0FF";
    //放款成功
    if ([status isEqualToString:@"rectist"]) {
        tagBgColor = @"#FBB6B6";
        tagtitleColor = @"#FF1919";
        
    } else if ([status isEqualToString:@"currsceneial"]) {
        // 逾期
        tagBgColor = @"#FBDDAE";
        tagtitleColor = @"#FF9C01";
    } else if ([status isEqualToString:@"aroundarium"]) {
        // 申请中
        tagBgColor = @"#C1E3FB";
        tagtitleColor = @"#40B0FF";
    } else if ([status isEqualToString:@"potam"]) {
        // 审或者放款中
        tagBgColor = @"#C3EDC8";
        tagtitleColor = @"#46D256";
    } else if ([status isEqualToString:@"phrenwise"]) {
        // 订单完成
        tagBgColor = @"#E5E5E5";
        tagtitleColor = @"#B8B8B8";
    }
    self.tagLabel.backgroundColor = [UIColor colorWithHexString:tagBgColor];
    self.tagLabel.textColor = [UIColor colorWithHexString:tagtitleColor];
}

@end
